package cn.kent.simple;

/**
 * 704. 二分查找
 */
public class Search {
    public static void main(String[] args) {
        int[] nums = {-1,2,3,4,6};
        System.out.println(search(nums, 6));
    }

    public static int search(int[] nums, int target) {
        int left= 0,right = nums.length-1;
        while (left <= right) {
            int mid = (left+right)/2;
            if (nums[mid] > target) {
                right = mid-1;
            } else if (nums[mid] == target) {
                return mid;
            } else { // nums[mid] > target
                left = mid+1;
            }
        }
        return -1;
    }
}
